﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;



namespace JumpingMan
{
   
    public partial class Form1 : Form
    {

        public System.Timers.Timer aTimer = new System.Timers.Timer();
        private DateTime mtrigger = DateTime.Now;
        TimeSpan timespan = new TimeSpan();
        Timer1 timer = new Timer1();
        int AntalBollarILuften = 0;
        int GlobalShit = 0;
        int GlobalShit2 = 0;
        long startTime;
        long interval = (long)TimeSpan.FromSeconds(1.0 / 100).TotalMilliseconds;
        Graphics graphics;
        Graphics imageGraphics;
        Image backBuffer;
        int clientWidth;
        int clientHeight;
        ListofPlayer listofplayers;
        ListofMobs listofmobs;
        Player player;
        Sound soundplayer = new Sound();
        Image image = Image.FromFile(@"greekflag.jpg");
        Image image2 = Image.FromFile(@"greekflag2.jpg");
        public bool started = false;
        Form2 form2;
        
        public Form1()
        {
            InitializeComponent();
            Init();                                            
        }
        //Game Loop that ticks in the game
        public void GameLoop()
        {
            timer.Start();
            while (this.Created)
            {
                
                startTime = timer.ElapsedMilliseconds;
                GameLogic();
                RenderScene();
                Application.DoEvents();
                while (timer.ElapsedMilliseconds - startTime < interval) ;
            }
        }
        //Game Logics
        private void GameLogic()
        {
            if (started == true)
            {
                label1.Show();
                label2.Show();              
                player.MoveMobToDirection();
                listofmobs.MoveMobs();
                started = player.CheckForCollision(listofmobs, soundplayer, label1);              
                timespan = DateTime.Now - mtrigger;
                label1.Text = string.Format("{0}:{1:00}", timespan.Minutes, timespan.Seconds);
                AngryLabelText();
                label2.Text = AntalBollarILuften.ToString();               
            }
            if (started == false)
            {
                AfterDefeatRestart();               
                
            }                             
        }
        //Drawing the game
        private void RenderScene()
        {           
            if (started)
            {
                imageGraphics.DrawImage(image, 0, 0);
                player.DrawPlayer(imageGraphics);
                listofmobs.DrawMobs(imageGraphics);
                this.BackgroundImage = backBuffer;
                this.Invalidate();
            }
           
        }
        //Fixing the background with backBuffer
        public void Init()
        {
            this.DoubleBuffered = true;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.ClientSize = new Size(450, 400);
            clientWidth = this.ClientRectangle.Width;
            clientHeight = this.ClientRectangle.Height;
            backBuffer = (Image)new Bitmap(clientWidth, clientHeight);
            graphics = this.CreateGraphics();          
            imageGraphics = Graphics.FromImage(backBuffer);                                      
        }   

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (started == true)
            {
                player.MoveMob(e);
           
                //Boost
                if (e.KeyCode == Keys.Space)
                {
                    player.direction.X = 0;
                    player.direction.Y = 0;
                }
                if (e.KeyCode == Keys.A)
                {
                    listofmobs.AddMob();
                }
           
            }
        }
        public void AddTrapWithTimer()
        {                                  
                aTimer.Interval = 5000;
                aTimer.Enabled = true;                                                        
        }
        //Event that happens every 5 seconds, Adding a ball to the game and counting up
        private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {                     
                listofmobs.AddMob();
                AntalBollarILuften++;                         
        }
        //Turning the label.Text red every 5 seconds(when mob is set into the game)
        public void AngryLabelText()
        {
            string str = label1.Text;
            int index = str.IndexOf(':');
            string result = str.Substring(index + 1, 2);
            int result1 = Convert.ToInt32(result);
            if (result1 % 5 == 0)
            {
                label1.ForeColor = Color.Red;
            }
            else
            {
                label1.ForeColor = Color.Green;
            }

        }
        //Clicking the startlabel from the menu
        private void HelpTheGreekLabel_Click(object sender, EventArgs e)
        {

            //Game started
            started = true;

            HidingMenu();
            //Play gameMusic
            soundplayer.PlayZorba();
            //Fixing the clock so it starts with 0
            mtrigger = DateTime.Now;
            //Making sure that aTimer gets 1 elapsed event
            if (GlobalShit == 0)
            {
                aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
                GlobalShit++;
            }
            //Add Trap(Ball/Mob) according to timer
            AddTrapWithTimer();
            //Creating/Resetting player and listofmobs
            player = new Player();
            listofmobs = new ListofMobs(0);
            if (textBoxName.Text == "") 
            {
                player.namn = "No name";
            }
            else
            {
                player.namn = textBoxName.Text;
            }
            textBoxName.ReadOnly = true;

        }
        //Setting label show to get menu, clearing AntalBollarILuften, Closing timer
        private void AfterDefeatRestart()
        {
            
            
            label1.Hide();
            label2.Hide();
            ExitLabel.Show();
            HighScoreLabel.Show();
            HelpTheGreekLabel.Show();
            pictureBoxEuro.Show();
            pictureBoxGreek.Show();
            pictureBoxBackground.Show();
            textBoxName.Show();
            labelName.Show();
            textBoxName.ReadOnly = false;
            AntalBollarILuften = 0;
            aTimer.Close();                                  
        }
        //Hiding the menu
        private void HidingMenu()
        {
            HelpTheGreekLabel.Hide();
            HighScoreLabel.Hide();
            ExitLabel.Hide();
            pictureBoxEuro.Hide();
            pictureBoxGreek.Hide();
            pictureBoxBackground.Hide();
            label1.Show();
            label2.Show();
            labelName.Hide();
            textBoxName.Hide();
        }

        private void ExitLabel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void HelpTheGreekLabel_MouseHover(object sender, EventArgs e)
        {
            HelpTheGreekLabel.ForeColor = Color.Gold;
        }

        private void HighScoreLabel_MouseHover(object sender, EventArgs e)
        {
            HighScoreLabel.ForeColor = Color.Gold;
        }

        private void ExitLabel_MouseHover(object sender, EventArgs e)
        {
            ExitLabel.ForeColor = Color.Gold;
        }

        private void HelpTheGreekLabel_MouseLeave(object sender, EventArgs e)
        {
            HelpTheGreekLabel.ForeColor = Color.Blue;
        }

        private void HighScoreLabel_MouseLeave(object sender, EventArgs e)
        {
            HighScoreLabel.ForeColor = Color.Blue;
            if (GlobalShit2 == 1)
            {
                form2.Close();
                GlobalShit2 = 0;
            }
        }

        private void ExitLabel_MouseLeave(object sender, EventArgs e)
        {
            ExitLabel.ForeColor = Color.Blue;
        }

        private void HighScoreLabel_Click(object sender, EventArgs e)
        {
            listofplayers = new ListofPlayer();
            listofplayers.ReadPlayersFromFile();
            listofplayers.SortTheListOfPlayer();
            form2 = new Form2();
            form2.NameingLabels(listofplayers);
            form2.TimeLabels(listofplayers);
            form2.Show();
            GlobalShit2 = 1;
            
            
            //MessageBox.Show(listofplayers.WritePlayerResultsToMessageBox());
        }

        private void pictureBoxBackground_Click(object sender, EventArgs e)
        {

        }
        
       
    }
}
